<?php
define('LIBERTY_TEMPLATE_LIST',"list");
define('LIBERTY_TEMPLATE_ITEM',"view");
define('LIBERTY_TEMPLATE_EDIT',"edit");

class LibertyAction {
	private static $actions = array();

	/**
	 * add a new action handler
	 *
	 * @param class $class
	 */
	static function addAction($class) {
		$i = new $class();
		$a = $i->registerActionHandler();
		if (!empty($a['action'])) {
			if (empty(LibertyAction::$actions['actions'][$a['action']])) {
				LibertyAction::$actions['actions'][$a['action']] = $i;
			}
			if (empty(LibertyAction::$actions['meta'][$a['action']])) {
				LibertyAction::$actions['meta'][$a['action']] = $a;
			}
		}
	}

	/**
	 * Get the meta information about the given action
	 *
	 * @param string $action
	 * @return array
	 */
	static function getActionMeta($action) {
		$i = null;
		if (empty(LibertyAction::$actions['meta'][$action])) {
			$i = new LibertyActionBase();
		} else {
			$i = LibertyAction::$actions['meta'][$action];
		}
		return $i;
	}

	/**
	 * Get the meta information about all registered actions
	 *
	 * @return array
	 */
	static function getMetaList() {
		$a = LibertyAction::$actions['meta'];
		ksort($a);
		return $a;
	}

	/**
	 * Get the action instance for the given action
	 *
	 * @param string $action
	 * @return LibertyActionBase
	 */
	static function getAction($action) {
		$i = null;
		if (empty(LibertyAction::$actions['actions'][$action])) {
			$i = new LibertyActionBase();
		} else {
			$i = LibertyAction::$actions['actions'][$action];
		}
		return $i;
	}
}

class LibertyActionBase {
	function registerActionHandler() {
		return array();
	}

	function getDefaultTpl() {
		return array(
		'tpl'=>"error.tpl",
		'title'=>"Parameter error"
		);
	}

	function prepTemplate() {
		global $gBitSmarty;
		$gBitSmarty->assign('msg', "No Action of this name has been registered");
		return array(
		'tpl'=>"error.tpl",
		'title'=>"Invalid Action"
		);
	}
}

class LibertyActionList extends LibertyActionBase {

	function registerActionHandler() {
		return array(
		'action'=>'list',
		'name'=>'List',
		'description'=>'List Content',
		);
	}

	function getType() {
		global $gLibertySystem;
		$type = array();
		if (!empty($_REQUEST['t'])) {
			if (!empty($gLibertySystem->mContentTypes[$_REQUEST['t']])) {
				$type = $gLibertySystem->mContentTypes[$_REQUEST['t']];
			}
		} elseif(@BitBase::verifyId( $_REQUEST['c'] ) ) {
			$c = LibertyBase::getLibertyObject( $_REQUEST['c'] );
			$type = $c->mType;
		}
		if (!empty($type)) {
			$path = constant(strtoupper($type['handler_package']).'_PKG_PATH');
			require_once($path.$type['handler_file']);
			$class = $type['handler_class'];
			$type['instance'] = new $class();
		}
		return $type;
	}

	function loadList($type) {
		global $gBitSmarty;

		if (!empty($type)) {
			$obj = $type['instance'];
			$list = $obj->getList($_REQUEST);
			foreach ($list['data'] as $k=>$c) {
				if (!$obj->hasUserPermissionToContent($c,'view')) {
					$list['data'][$k]=null;
					unset($list['data'][$k]);
				}
			}
			$gBitSmarty->assign_by_ref('listInfo', $list['listInfo']);
			$gBitSmarty->assign_by_ref('list', $list);
			return $list;
		}
		return array();
	}

	function prepTemplate() {
		global $gBitSystem;
		$rTemplate=$this->getDefaultTpl();
		$type = $this->getType();
		if (!empty($type)) {
			$list = $this->loadList($type);
			$obj = $type['instance'];
			$template = $obj->getTemplate(LIBERTY_TEMPLATE_LIST);
			if (!empty($template['php'])) {
				require_once($template['php']);
			}

			$title =null;
			if (isset($template['title'])) {
				$title = $template['title'];
			}
			$rTemplate['tpl']=$template['tpl'];
			$rTemplate['title']=$title;
		} else {
			$gBitSmarty->assign('msg', "Invalid Type");
			$rTemplate['title']="Parameter error";
		}
		return $rTemplate;
	}
}

class LibertyActionView extends LibertyActionBase {

	function registerActionHandler() {
		return array(
		'action'=>'view',
		'name'=>'View',
		'description'=>'View Content',
		);
	}

	function loadContent() {
		global $gBitSmarty;
		$rV = array(
		'content'=>null,
		'access'=>true,
		);
		require_once( LIBERTY_PKG_PATH.'LibertyBase.php');
		if( @BitBase::verifyId( $_REQUEST['c'] )  && $c = LibertyBase::getLibertyObject( $_REQUEST['c'] ) ) {
			$_REQUEST['content_id']=$_REQUEST['c'];
			if ($c->hasUserPermission('view')) {
				$c->parseData();
				$gBitSmarty->assign_by_ref( 'gContent', $c );
				$gBitSmarty->assign_by_ref( 'pageInfo', $c->mInfo );
				$rV['content']=$c;
			} else {
				$rV['access']=false;
				unset($c);
			}
		}
		return $rV;
	}

	function getTitle($template,$gContent) {
		$title =null;
		if (isset($template['title'])) {
			$title = $template['title'];
		}
		if (isset($gContent->mInfo['title'])) {
			$title = $gContent->mInfo['title'];
		}
		return $title;
	}

	function loadTemplate($gContent) {
		global $gBitSmarty;
		global $gBitSystem;
		$template = $gContent->getTemplate(LIBERTY_TEMPLATE_ITEM);
		if (!empty($template['php'])) {
			require_once($template['php']);
		}
		if (!empty($gContent->mInfo['last_modified'])) {
			//header('Last-Modified: ' .date('r',$gContent->mInfo['last_modified']));
		}
		return $template;
	}

	function prepTemplate() {
		global $gBitSystem;
		global $gBitSmarty;
		$rTemplate=$this->getDefaultTpl();

		$v = $this->loadContent();
		if (!$v['access']) {
			$gBitSmarty->assign('msg', "You don't have access to this content");
			$rTemplate['title']="Access Restriction";
		} elseif (empty($v['content'])) {

		} else {
			$template = $this->loadTemplate($v['content']);
			$rTemplate['tpl']=$template['tpl'];
			$rTemplate['title']=$this->getTitle($template,$v['content']);
		}

		return $rTemplate;
	}
}

class LibertyActionEdit extends LibertyActionView {

	function registerActionHandler() {
		return array(
		'action'=>'edit',
		'name'=>"Edit",
		'description'=>'Edit Content',
		);
	}

	function loadContent() {
		$rV = parent::loadContent();
		if (! $rV['content']->hasUserPermission('edit')) {
			$rV['access']=false;
			unset($rV['content']);
			$rV['content']=null;
		}
		return $rV;
	}

	function loadTemplate($gContent) {
		$template = $gContent->getTemplate(LIBERTY_TEMPLATE_EDIT);
		if (!empty($template['php'])) {
			require_once($template['php']);
		}
		if (!empty($gContent->mInfo['last_modified'])) {
			//header('Last-Modified: ' .date('r',$gContent->mInfo['last_modified']));
		}
		return $template;
	}

}

/**
 * Enter description here...
 *
 * @param array $pMixed
 */
function xmlLibertyContent($pMixed,$obj,$head=true) {
	if (is_object($pMixed)) {
		return xmlLibertyContent($pMixed->mInfo,$head);
	}
	$s = '';
	if ($head) {
		$s .= "<?xml version = \"1.0\"?>";
	}
	if (!empty($pMixed['data']) && !empty($pMixed['format_guid']) && empty($pMixed['parsed_data'])) {
		global $gLibertySystem;
		if( $func = $gLibertySystem->getPluginFunction( $pMixed['format_guid'], 'load_function' ) ) {
			$pMixed['parsed_data'] = $func( $pMixed, $obj );
		}
	}
	$s .="<libertycontent";
	if (!empty($pMixed['content_type_guid'])) {
		$s .=" content_type_guid=\"{$pMixed['content_type_guid']}\"";
		unset($pMixed['content_type_guid']);
	}
	if (!empty($pMixed['content_id'])) {
		$s .=" content_id=\"{$pMixed['content_id']}\"";
		unset($pMixed['content_id']);
	}
	$s .=">";
	/*
	if (!empty($pMixed['format_guid'])) {
		$v = htmlentities($pMixed['format_guid']);
		$s .="<format_guid>$v</format_guid>";
	}
	if (!empty($pMixed['data'])) {
		$v = htmlentities($pMixed['data']);
		$s .="<data>$v</data>";
	}
	if (!empty($pMixed['data']) && !empty($pMixed['format_guid']) && empty($pMixed['parsed_data'])) {
		global $gLibertySystem;
		if( $func = $gLibertySystem->getPluginFunction( $pMixed['format_guid'], 'load_function' ) ) {
			$pMixed['parsed_data'] = $func( $pMixed, $obj );
		}
	}
	if (!empty($pMixed['parsed_data'])) {
		$v = htmlentities($pMixed['parsed_data']);
		$s .="<parsed_data>$v</parsed_data>";
	}
	if (!empty($pMixed['title'])) {
		$v = htmlentities($pMixed['title']);
		$s .="<title>$v</title>";
	}
	if (!empty($pMixed['content_description'])) {
		$v = htmlentities($pMixed['content_description']);
		$s .="<content_description>$v</content_description>";
	}
	if (!empty($pMixed['last_modified'])) {
		$v = htmlentities($pMixed['last_modified']);
		$s .="<last_modified>$v</last_modified>";
	}*/
	foreach ($pMixed as $key => $value) {
		$v = htmlentities($value);
		$s .="<$key>$v</$key>\n";
	}
	$s .="</libertycontent>";
	return $s;
}

/**
 * Enter description here...
 *
 * @param string $a
 */
function jsArray($a) {

}

class LibertyActionXList extends LibertyActionList  {
	function registerActionHandler() {
		return array(
		'action'=>'xlist',
		'name'=>'XML List',
		'description'=>'List Content XML',
		);
	}

	function export($data,$obj) {
		header( 'Content-Type: text/xml; charset=utf-8' );
		$s = '<?xml version = "1.0"?>';
		$s .="<content_list>";
		foreach ($data as $item) {
			$s .=xmlLibertyContent($item,$obj,false);
		}
		$s .="</content_list>";
		return $s;
	}

	function loadList($type) {
		global $gBitSmarty;

		if (!empty($type)) {
			$obj = new LibertyContent();
			$pListHash =array();
			$pListHash['content_type_guid'] = $type['content_type_guid'];
			$pListHash['load_data']=true;
			$list = $obj->getContentList($pListHash);
			foreach ($list['data'] as $k=>$c) {
				if (!$obj->hasUserPermissionToContent($c,'view')) {
					$list['data'][$k]=null;
					unset($list['data'][$k]);
				}
			}
			return $list;
		}
		return array();
	}

	function prepTemplate() {
		global $gBitSystem;
		$rTemplate=$this->getDefaultTpl();
		$type = $this->getType();
		if (!empty($type)) {
			$list = $this->loadList($type);
			$obj = $type['instance'];
			unset($rTemplate['tpl']);
			//vd($list['data']);
			$rTemplate['raw']=$this->export($list['data'],$obj);
		} else {
			$gBitSmarty->assign('msg', "Invalid Type");
			$rTemplate['title']="Parameter error";
		}
		return $rTemplate;
	}
}

class LibertyActionJSList extends LibertyActionXList  {
	function registerActionHandler() {
		return array(
		'action'=>'jsonlist',
		'name'=>'JSON List',
		'description'=>'List Content JSON',
		);
	}

	function export($data,$obj) {
		header( 'Content-Type: text/javascript; charset=utf-8' );
		$s ="[\n";
		foreach ($data as $item) {
			$e =var_export($item,true);
			$e =str_replace("array (","{",$e);
			$e =str_replace(" =>",":",$e);
			$s .= substr($e,0,-1)."},";
		}
		$s .="]";
		return $s;
	}
}

LibertyAction::addAction('LibertyActionList');
LibertyAction::addAction('LibertyActionView');
LibertyAction::addAction('LibertyActionEdit');
LibertyAction::addAction('LibertyActionXList');
LibertyAction::addAction('LibertyActionJSList');

require_once( "../bit_setup_inc.php" );
$gBitSmarty->assign('xa',true);
$action = 'view';
if (!empty($_REQUEST['action'])) {
	$action = $_REQUEST['action'];
}
$a = LibertyAction::getAction($action);
$template = $a->prepTemplate();
if (empty($template['tpl']) && !isset($template['raw'])) {
	$gBitSmarty->assign('msg', "No Data was returned");
	$template['tpl']="error.tpl";
	$template['title']="Internal Error";
}
if (!empty($template['tpl'])) {
	$gBitSystem->display($template['tpl'],$template['title']);
} else {
	echo $template['raw'];
}
?>
